#include<iostream>
#include<algorithm>
using namespace std;

const int N=1e6+10;
int l[N],r[N];
int dfs(int u)
{
	if(u==0) return 0;
	int left=dfs(l[u]);
	int right=dfs(r[u]);
	return max(left,right)+1;
}
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>l[i]>>r[i];
	}
	cout<<dfs(1);
	return 0;
}